package com.yx.filemanager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yx.filemanager.util.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by yx on 2017/8/31 0031.
 */

public class FileActivity extends AppCompatActivity {
    private List<FileInfo> fileList;
    private List<List<FileInfo>> recordFiles;
    private String path;
    private FileAdapter fileAdapter;
    private int lastPosition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 无标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        CustomUtil.setTranslucentStatus(this, R.color.colorPrimary);
        setContentView(R.layout.activity_file);
        path = getIntent().getStringExtra("path");

        fileList = new ArrayList<>();
        recordFiles = new ArrayList<>();
        RecyclerView recycleview = (RecyclerView) findViewById(R.id.recycleview);
        recycleview.setLayoutManager(new LinearLayoutManager(this));
        fileAdapter = new FileAdapter(fileList);
        recycleview.setAdapter(fileAdapter);
        recycleview.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (fileAdapter.getData().get(position).isDir()) {
                    List<FileInfo> recordList = new ArrayList<>();
                    recordList.addAll(fileAdapter.getData());
                    recordFiles.add(recordList);
                    path = fileAdapter.getData().get(position).getFilePath();
                    queryData();
                }
            }
        });

        queryData();


    }

    private void queryData() {
        fileList.clear();
        Observable.just(path)
                .map(new Func1<String, File>() {
                    @Override
                    public File call(String s) {
                        return new File(path);
                    }
                })
                .flatMap(new Func1<File, Observable<File>>() {
                    @Override
                    public Observable<File> call(File file) {
                        return Observable.from(file.listFiles());
                    }
                })
//                .filter(new Func1<File, Boolean>() {
//                    @Override
//                    public Boolean call(File file) {
//                        return file.getName().endsWith(".png");
//                    }
//                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<File>() {
            @Override
            public void onCompleted() {
                fileAdapter.setNewData(fileList);
                fileAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(File file) {
                FileInfo fileInfo = new FileInfo();
                if (FileUtils.isDir(file)) {
                    fileInfo.setDir(true);
                } else {
                    fileInfo.setDir(false);
//                    fileInfo.setFileSize(FileUtils.getFileSize(file));
//                    fileInfo.setExt(FileUtils.getExtensionName(file.getName()));
                }
                fileInfo.setFileName(file.getName());
                fileInfo.setFilePath(file.getAbsolutePath());
                fileList.add(fileInfo);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (recordFiles.size() >= 1) {
            //回到上个页面数据
            lastPosition = recordFiles.size() - 1;
            fileAdapter.setNewData(recordFiles.get(lastPosition));
            fileAdapter.notifyDataSetChanged();
            //已经回到当前页面数据，要移除上一个记录数据
            lastPosition = recordFiles.size() - 1;
            recordFiles.remove(lastPosition);
        } else {
            super.onBackPressed();
        }
    }
}
